fix: handle pydantic ValidationError in beta.chat.completions.parse (fixes #1763)#2917
fix: handle pydantic ValidationError in beta.chat.completions.parse (fixes #1763)#2917giulio-leone wants to merge 1 commit intoopenai:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a dedicated SDK error for structured-output parsing failures so users get a clear, actionable exception (with access to the raw model content) instead of a bare pydantic.ValidationError when chat.completions.parse() receives malformed/truncated JSON.
Changes:
- Introduces
ContentFormatError(subclass ofOpenAIError) to represent response-format parsing failures. - Wraps Pydantic parsing in
_parse_content()to raiseContentFormatErroronpydantic.ValidationError/json.JSONDecodeError. - Adds regression tests covering malformed JSON and schema mismatch cases, and exports the new exception from
openai.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/openai/lib/_parsing/_completions.py |
Wraps response-format parsing to raise a consistent SDK exception instead of leaking raw Pydantic errors. |
src/openai/_exceptions.py |
Adds ContentFormatError carrying raw_content for debugging. |
src/openai/__init__.py |
Exports ContentFormatError from the top-level package. |
tests/lib/chat/test_completions.py |
Adds tests asserting ContentFormatError and raw_content preservation for malformed JSON and schema mismatch. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Thanks for the thorough review! All feedback has been addressed in 81f3e5b:
|
Include truncated raw_content (first 500 chars) in exception message for better debugging while avoiding unbounded output. Full content remains accessible via the raw_content attribute. Update tests to reflect the new message format and add truncation coverage test. Refs: openai#2917
121799e to
4290c9a
Compare
78ef382 to
8e73ff6
Compare
|
Intervention note for this PR: Current blocker appears to be missing CI execution rather than failing jobs:
Suggested unblock sequence:
If useful, I can run a follow-up status sweep as soon as checks are attached. |
Problem
When
beta.chat.completions.parse()receives malformed or truncated JSON from the API (withfinish_reason: "stop"), a rawpydantic.ValidationErroris raised directly to the user with no context about what went wrong or what the original content was.This is confusing because:
Solution
Added a new
ContentFormatErrorexception (extendsOpenAIError, following the pattern ofLengthFinishReasonError) that wraps bothpydantic.ValidationErrorandjson.JSONDecodeErrorin_parse_content().The new error:
raw_contentattribute for debugging__cause__so the full traceback is availableChanges
src/openai/_exceptions.py: AddedContentFormatErrorclasssrc/openai/__init__.py: ExportedContentFormatErrorsrc/openai/lib/_parsing/_completions.py: Wrapped_parse_content()with try/excepttests/lib/chat/test_completions.py: Added tests for malformed JSON and schema mismatchExample
Fixes #1763